home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / reflscan.c < prev    next >
Text File  |  1998-07-17  |  9KB  |  406 lines

  1. /*
  2.  *   yet another pointless and retarded proggie brought to you by
  3.  *                       reflector
  4.  *   thx to halflife for psuedo header and timer shit from halfscan.c
  5.  */
  6. #include <sys/types.h> 
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <netinet/in.h>
  11. #include <sys/socket.h>
  12. #include <linux/ip.h>
  13. #include <linux/tcp.h>
  14. #include <signal.h>
  15. #include <netdb.h>
  16. #include <arpa/inet.h>
  17. #include <unistd.h>
  18. #include <signal.h>
  19. #include <errno.h>
  20.  
  21. #define MAXHOSTNAME   128
  22.  
  23. int timeout = 0;
  24. int lazy_refl = 0;
  25.  
  26. unsigned short checkzum(unsigned short *, int);
  27. void dumpsyns(int, unsigned, unsigned, int, unsigned, unsigned, int);
  28. void getdemsyns(unsigned, unsigned, int, int, char *);
  29. void sort_ports(int *, int, FILE *, int);
  30. void alarm_handler(int);
  31. void refl_handler(int);
  32. int compar(const void *, const void *);
  33.  
  34. main (argc, argv)
  35. int argc;
  36. char *argv[];
  37.  
  38. {
  39.  
  40.    int r,e,f,l,dirty_sock;
  41.    pid_t dapid;
  42.    char *filename;
  43.    char *localhost;
  44.    unsigned max_port;
  45.    unsigned dest;
  46.    unsigned source;
  47.    struct hostent *he;
  48.    f=0;
  49.    l=0;
  50.    
  51.    
  52.    /* check usage */
  53.    
  54.    if ((argc < 4) || (argc > 6))
  55.    {
  56.       printf("usage: %s source target max-port-to-scan [-f filename]\n", argv[0]);
  57.       printf("       source must be YOUR ip address as a dotted quad.\n");
  58.       exit(1);
  59.    }
  60.  
  61.    for (r=0; r < argc; r++)
  62.    {
  63.       if(!strcmp("-f", argv[r]))
  64.       {
  65.          f=1;
  66.          filename = argv[++r];
  67.       }
  68.    }
  69.      
  70.    max_port = atoi(argv[3]);
  71.    if ((max_port < 1) || (max_port > 32767))
  72.    {
  73.       printf("max port to scan should really be between 1 and 32,767.\n");
  74.       exit(1);
  75.    }
  76.  
  77.    dest = inet_addr(argv[2]);
  78.    if (dest == -1);
  79.    {
  80.       if((he = gethostbyname(argv[2])) == NULL)
  81.       {
  82.          printf("doh.  %s duz not exist.  cunt.\n", argv[2]);
  83.      exit(1);
  84.       }
  85.       bcopy ((char *)he->h_addr,  (char *)&dest, he->h_length);
  86.    }
  87.    
  88.  
  89.    source = inet_addr(argv[1]);
  90.    if (source == -1)
  91.    {
  92.       printf("check that source address you fucking dork.\n");
  93.       exit(1);
  94.    }
  95.  
  96.  
  97.    /* open raw sockets */
  98.  
  99.    if ((dirty_sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0)
  100.    {
  101.       printf("could not open socket. are you even root, cunt?\n");
  102.       exit(1);
  103.  
  104.    }
  105.    
  106.    printf("checking out ports 1 thru %d on %s.\n", max_port, argv[2]);
  107.  
  108.    /* fork - parent sends syns, child listens, rsts half open connections. */
  109.  
  110.    dapid = fork();
  111.    if (dapid < 0)
  112.    {
  113.       printf("could not fork.\n");
  114.       exit(1);
  115.    }
  116.    if (dapid > 0)
  117.    {
  118.       signal(SIGALRM, alarm_handler);
  119.       sleep(3);
  120.       for (r=1; r <= max_port; r++)
  121.          dumpsyns(r, source, dest, dirty_sock, 0, 0, 0);
  122.       kill(dapid, 10);      
  123.       while (1)
  124.       {
  125.          if (timeout == 1)
  126.      {
  127.         sleep(2);
  128.         exit(1);
  129.      }
  130.       }
  131.    }
  132.    if (dapid == 0)
  133.    {
  134.      getdemsyns(source, dest, dirty_sock, f, filename); 
  135.    }
  136. }
  137.  
  138. void dumpsyns(int port, unsigned source, unsigned dest, int dirty_sock, unsigned seqnum, unsigned acknum, int testrst)
  139. {
  140.    int r,e,f,doh;
  141.    struct sockaddr_in sin;
  142.    struct tcphdr teeseepee;
  143.    struct fux_psuedo_heder
  144.    {
  145.       unsigned source;
  146.       unsigned dest;
  147.       unsigned pad     :  8,
  148.                proto   :  8,
  149.            tcp_len : 16;
  150.       struct tcphdr tcp;
  151.    }psuedo_hed;
  152.  
  153.  
  154.    sin.sin_family      = AF_INET;
  155.    sin.sin_port        = htons(port);
  156.    sin.sin_addr.s_addr = dest;
  157.    
  158.    /* fill in tcp header, checking if we want this to be a rst segment. */
  159.  
  160.    teeseepee.source    = getpid();
  161.    teeseepee.dest      = htons(port);
  162.  
  163.    /* if this is to be a rst */
  164.  
  165.    if (testrst == 1)
  166.    {
  167.       teeseepee.seq       = acknum;
  168.       teeseepee.ack_seq   = seqnum + 1;
  169.       teeseepee.rst       = 1;
  170.       teeseepee.syn       = 0;
  171.       teeseepee.ack       = 1;
  172.       teeseepee.window    = 0;
  173.    }
  174.  
  175.    /* if not */
  176.  
  177.    if (testrst == 0)
  178.    {
  179.       teeseepee.seq       = getpid() + port;
  180.       teeseepee.ack_seq   = 0;
  181.       teeseepee.rst       = 0;
  182.       teeseepee.syn       = 1;
  183.       teeseepee.ack       = 0;
  184.       teeseepee.window    = htons(512);
  185.    }
  186.  
  187.    /* these don't care if we are rsting or not */
  188.  
  189.    teeseepee.doff      = 5;
  190.    teeseepee.res1      = 0;
  191.    teeseepee.res2      = 0;
  192.    teeseepee.urg       = 0;
  193.    teeseepee.psh       = 0;
  194.    teeseepee.fin       = 0;
  195.    teeseepee.check     = 0;
  196.    teeseepee.urg_ptr   = 0;
  197.  
  198.    /* fill in psuedo-header.  ugh. */
  199.  
  200.    psuedo_hed.source   = source;
  201.    psuedo_hed.dest     = dest;
  202.    psuedo_hed.pad      = 0;
  203.    psuedo_hed.proto    = 6;
  204.    psuedo_hed.tcp_len  = htons(20);
  205.  
  206.    bcopy (&teeseepee, (char *)&psuedo_hed.tcp, 20);
  207.    teeseepee.check = checkzum((unsigned short *)&psuedo_hed, 32);
  208.  
  209.    /* bewm! */
  210.  
  211.    doh = sendto(dirty_sock, &teeseepee, sizeof(teeseepee), 0, (struct sockaddr *)&sin, sizeof(sin));
  212.    if (doh != sizeof(teeseepee))
  213.    {
  214.       printf("error sending syn.\n");
  215.       exit(1);
  216.    }
  217.    
  218. }
  219.  
  220. void getdemsyns(source, dest, stiff_sock, f, filename)
  221. unsigned source,dest;
  222. int stiff_sock, f;
  223. char *filename;
  224. {
  225.  
  226.    int rst_sock;
  227.    int doh, r, e=0, i=1;
  228.    int array[150];
  229.    FILE *fp;
  230.    struct servent *sp;
  231.    struct sockaddr_in sin, duhsin;
  232.    struct in_addr ina;
  233.    struct in_tcp
  234.    {
  235.       struct iphdr   ip;
  236.       struct tcphdr tcp;
  237.       char dataz[65495];
  238.    }intcp;
  239.  
  240.    ina.s_addr = dest;
  241.  
  242.    if (f == 1)
  243.    {
  244.       if ((fp = fopen(filename, "a")) == NULL)
  245.       {
  246.          printf("could not open file, cunt.\n");
  247.          exit(1);
  248.       }
  249.       fprintf(fp, "            --- results for %s ---\n", inet_ntoa(ina));
  250.    }
  251.  
  252.    duhsin.sin_family      = AF_INET;
  253.    duhsin.sin_port        = htons(getppid());
  254.    duhsin.sin_addr.s_addr = source;
  255.  
  256.    if ((bind(stiff_sock, (struct sockaddr *)&duhsin, sizeof(duhsin))) < 0)
  257.    {
  258.       printf("could not bind socket.\n");
  259.       exit(1);
  260.    }   
  261.  
  262.    sin.sin_family      = AF_INET;
  263.    sin.sin_port        = htons(getppid());
  264.    sin.sin_addr.s_addr = dest;
  265.   
  266.    
  267.    if ((rst_sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0)
  268.    {
  269.       printf("error opening rst_sock.\n");
  270.       exit(1);
  271.    }
  272.  
  273.    
  274.    signal(SIGUSR1, refl_handler);
  275.    signal(SIGALRM, alarm_handler);
  276.    alarm(30);
  277.    printf("        SYNs received [");
  278.    fflush(stdout);
  279.    while(1)
  280.    {
  281.       if ((timeout == 1) && (lazy_refl == 1))
  282.       {
  283.          printf("]\n");
  284.          sort_ports(array, f, fp, i);
  285.       }
  286.       doh = read(stiff_sock, (char *)&intcp, 65535);  
  287.       if (intcp.tcp.dest == getppid())
  288.       {
  289.          if (intcp.tcp.syn == 1)
  290.          {
  291.         alarm(35);
  292.          
  293.         /* reset connection */
  294.  
  295.         dumpsyns(intcp.tcp.source, source, dest, rst_sock, intcp.tcp.ack_seq, intcp.tcp.ack, 1); 
  296.  
  297.             /* stuff port in array */
  298.  
  299.             e = 0;
  300.  
  301.             for(r=0; r <= i; r++)
  302.         {
  303.            if(ntohs(intcp.tcp.source) == array[r])
  304.               e = 1;
  305.         }
  306.  
  307.             if (e == 0)
  308.             {
  309.                printf(".");
  310.                fflush(stdout);
  311.                array[i] = ntohs(intcp.tcp.source); 
  312.                i++;
  313.             }                 
  314.          }             
  315.       }        
  316.    }        
  317. }   
  318.  
  319. void sort_ports(int *array, int f, FILE *fp, int i)
  320. {
  321.    pid_t pidz;
  322.    int r,e;
  323.    struct servent *sp;
  324.    
  325.    setservent(1);
  326.    pidz = getppid();
  327.    fflush(stdout);
  328.    qsort(array, i, sizeof(array[0]), compar);
  329.    for(e = 1; e < i; e++)
  330.    {
  331.       if ((sp = getservbyport(array[e], "tcp")) == NULL)
  332.       {
  333.          if (f == 1)
  334.             fprintf(fp, "received syn from port %d.\n", array[e]);
  335.          else
  336.             printf("received syn from port %d.\n", array[e]);
  337.       }
  338.       else
  339.       {
  340.          if (f == 1)
  341.             fprintf(fp, "%s found on port %d.\n", sp->s_name, array[e]);
  342.          else
  343.             printf("%s found on port %d.\n", (char *)sp->s_name, array[e]);
  344.       }
  345.       fflush(stdout);
  346.    }
  347.    kill(pidz, 14);
  348.    endservent();
  349.    exit(1);
  350. }
  351.  
  352. int compar(const void *intone, const void *inttwo)
  353. {
  354.    if (*(int *)intone == *(int *)inttwo)
  355.       return 0;
  356.    if (*(int *)intone < *(int *)inttwo)
  357.       return -1;
  358.    if (*(int *)intone > *(int *)inttwo)
  359.       return 1;
  360. }
  361.  
  362. void alarm_handler(int s)
  363. {
  364.    alarm(0);
  365.    timeout = 1;
  366. }
  367.  
  368. void refl_handler(int s)
  369. {
  370.    lazy_refl = 1;
  371. }
  372.  
  373. /*
  374.  * I didn't steal this from ping.c.  really.  I wrote it.  really.
  375.  */                            
  376. unsigned short checkzum(addr, len)
  377.     u_short *addr;
  378.     int len;
  379. {
  380.     register int nleft = len;
  381.     register u_short *w = addr;
  382.     register int sum = 0;
  383.     u_short answer = 0;
  384.  
  385.     /*
  386.      * Our algorithm is simple, using a 32 bit accumulator (sum), we add
  387.      * sequential 16 bit words to it, and at the end, fold back all the
  388.      * carry bits from the top 16 bits into the lower 16 bits.
  389.      */
  390.     while (nleft > 1)  {
  391.         sum += *w++;
  392.         nleft -= 2;
  393.     }
  394.     /* mop up an odd byte, if necessary */
  395.     if (nleft == 1) {
  396.         *(u_char *)(&answer) = *(u_char *)w ;
  397.         sum += answer;
  398.     }
  399.  
  400.     /* add back carry outs from top 16 bits to low 16 bits */
  401.     sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  402.     sum += (sum >> 16);         /* add carry */
  403.     answer = ~sum;              /* truncate to 16 bits */
  404.     return(answer);
  405. }
  406.